﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IntellectShop.Web2.ViewModels.CartViewModel>" %>
<%@ Import Namespace="IntellectShop.Web2.Code" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	<%= Html.Resource("CartIndex") %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
	<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
	<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
	<script type="text/javascript">
		function handleUpdate(context) {
			// Load and deserialize the returned JSON data
			var json = context.get_data();
			var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

			// Update the page elements
			$('#row-' + data.DeleteId + '-countCell').text(data.RemainingItemsCount);
			if (data.RemainingItemsCount == 0) {
				$('#row-' + data.DeleteId).fadeOut('slow');
			}

			var cartName = '<%= Html.Resource("Strings, Cart") %>';
			$('#cart-status').text(cartName + ' (' + data.CartCount + ')');

			$('#update-message').html(data.Message);
			$('#cart-total-count').text(data.CartCount);
			$('#cart-total').text(data.CartTotal);

			if (data.CartCount == 0) {
				$('#ctlCreateOrder').fadeOut('slow');
			}
		}
	</script>
	<h1>
		<%= Html.Resource("ReviewYourCart")%>:
	</h1>
	<div class="innerArticle">
		<% Html.RenderAction("ViewBody", "PageContent", new { pageContentUniqName = IntellectShop.Common.PageContentUniqName.CartIndex }); %>
	</div>

	<div id="update-message">
	</div>
	<% if (Model.CartItems.Any())
	{%>
	<table>
		<tr>
			<th>
				<%= Html.Resource("Code")%>
			</th>
			<th>
				<%= Html.Resource("Name")%>
			</th>
			<th>
				<%= Html.Resource("PriceEach")%>
			</th>
			<th>
				<%= Html.Resource("Quantity")%>
			</th>
			<th>
				<%= Html.Resource("Actions")%>
			</th>
		</tr>
		<% foreach (var item in Model.CartItems)
		{ %>
		<tr id="row-<%= Html.Encode(item.CartItemID) %>">
			<td style="text-align: right; white-space:nowrap;">
				<%= Html.ActionLink(item.Commodity.Code, "Details", "Store", new { id = item.Commodity.CommodityID }, null)%>
			</td>
			<td>
				<%= item.Commodity.Name %>
			</td>
			<td style="text-align:right;">
				<%= Html.Encode(item.Commodity.Price) %>
			</td>
			<td id="row-<%= Html.Encode(item.CartItemID) %>-countCell" style="text-align:right;">
				<%= Html.Encode(item.Count) %>
			</td>
			<td>
				<%= Ajax.ActionLink(Html.Resource("RemoveFromCart"), "RemoveFromCart", new { id = item.CartItemID }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "handleUpdate" })%>
			</td>
		</tr>
		<% } %>
		<tr class="total">
			<th colspan="2">
				<%= Html.Resource("Total")%>:
			</th>
			<th id="cart-total">
				<%= Html.Encode(Model.CartTotal) %>
			</th>
			<th id="cart-total-count">
				<%= Html.Encode(Model.CartCount) %>
			</th>
			<th >
				&nbsp;
			</th>
		</tr>
	</table>
	<p id="ctlCreateOrder">
		<%= Html.ActionLink(Html.Resource("CreateOrder"), "Create", "Order")%>
	</p>
	<%} else {%>
	<p>
		<%= Html.Encode(Html.Resource("CartIsEmpty")) %>
	</p>
	<% } %>
</asp:Content>
